package com.chat.room.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChatUserInfo implements Serializable {
    /*昵称*/
    private String nickName;
    /*IP地址*/
    private InetAddress ip;
    /*端口号*/
    private int port;

    private Socket accept;

    private PrintStream printStream;

    private BufferedReader bufferedReader;

    public ChatUserInfo(String nickName, InetAddress ip, int port, Socket accept) {
        this.nickName = nickName;
        this.ip = ip;
        this.port = port;
        this.accept = accept;
    }

    public PrintStream getPrintStream() {
        if (printStream == null) {
            try {
                printStream = new PrintStream(accept.getOutputStream());
            } catch (IOException e) {
                return null;
            }
        }
        return printStream;
    }

    public BufferedReader getBufferReader() {
        if (null == bufferedReader) {
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
            } catch (IOException e) {
                return null;
            }
        }
        return bufferedReader;
    }


    @Override
    public String toString() {
        return "ChatUserInfo{" +
                "nickName='" + nickName + '\'' +
                ", ip=" + ip.getHostName() +
                ", port=" + port +
                '}';
    }
}